package com.shenmazong.servereduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servereduuser.entity.TbRole;
import com.shenmazong.servereduuser.mapper.TbRoleMapper;
import com.shenmazong.servereduuser.service.TbRoleService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbRoleServiceImpl implements TbRoleService {
    @Autowired
    private TbRoleMapper roleMapper;
    @Override
    public ResultResponse addRole(TbRole tbRole) {
        TbRole role = roleMapper.selectOne(
                new QueryWrapper<TbRole>().lambda().eq(TbRole::getRoleName, tbRole.getRoleName()).last("limit 1")
        );
        if(role!=null){
            return ResultResponse.FAILED(404,"角色已经存在");
        }
        try {
            roleMapper.insert(tbRole);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"添加角色失败");
        }

        return ResultResponse.SUCCESS(200,"添加角色记录成功");
    }

    @Override
    public ResultResponse deleteRole(BatchChooseIdVo idVo) {
        if(idVo.getIds().size()<=0){
            return ResultResponse.FAILED(404,"请至少选择一个角色");
        }

        try {
            roleMapper.deleteBatchIds(idVo.getIds());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"删除角色失败");
        }
        return ResultResponse.SUCCESS(200,"删除角色记录成功");
    }

    @Override
    public ResultResponse getOneRole(TbRole tbRole) {
        if(tbRole.getRoleId()==null ||tbRole.getRoleId()==0){
            return ResultResponse.FAILED(404,"角色id不能为null");
        }
        TbRole role = roleMapper.selectOne(
                new QueryWrapper<TbRole>().lambda().eq(TbRole::getRoleName, tbRole.getRoleName()).last("limit 1")
        );
        if(role==null){
            return ResultResponse.FAILED(404,"角色不存在");
        }

        return ResultResponse.SUCCESS(role);
    }

    @Override
    public ResultResponse updateRole(TbRole tbRole) {
        if(tbRole.getRoleId()==null ||tbRole.getRoleId()==0){
            return ResultResponse.FAILED(404,"角色id不能为null");
        }
        try {
            roleMapper.updateById(tbRole);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED(500,"更新角色失败");
        }
        return ResultResponse.SUCCESS(200,"更新角色成功");
    }
}




